﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Wild2.Models.Exceptions;

namespace Wild2.Controllers
{
    public class TopicController : Controller
    {
        public ActionResult Index(int id)
        {
            int tid = id;
            if (tid < 0) tid = Wild2.Models.Topic.Topic.GetTopicIDFromMID(Math.Abs(id));

            Wild2.Models.Pages.Topic.Index page = new Models.Pages.Topic.Index();
            page.Topic = Wild2.Models.Topic.Topic.GetTopicInfo(tid);
            if (Wild2.Models.Auth.AuthOnSite.GetUserId().HasValue) Models.Favorites.Favorites.AddFavoriteToSession("Тема из дневников " + page.Topic.Caption);
            if (Request.QueryString["votes"] != null)
            {
                page.Votes = Wild2.Models.Topic.Topic.GetVotesList(tid);
            }
            page.Comments = Wild2.Models.Topic.Topic.GetTopicComments(tid);
            return View(page);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(int id, string message)
        {
            int tid = id;
            if (tid < 0) tid = Wild2.Models.Topic.Topic.GetTopicIDFromMID(Math.Abs(id));

            Wild2.Models.Pages.Topic.Index page = new Models.Pages.Topic.Index();
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (UserID.HasValue)
            {
                if (Wild2.Models.Text.Helper.GetLastMessage() != message)
                {
                    int result = 0;
                    if (id > 0)
                        try
                        {
                            result = Wild2.Models.Topic.Topic.AddTopicComment(UserID.Value, tid, message);
                        }
                        catch (TextException e)
                        {
                            page.infoMessage = e.Message;
                        }
                    else
                        try
                        {
                            result = Wild2.Models.Topic.Topic.AddTopicCommentAnswer(UserID.Value, Math.Abs(id), message);
                        }
                        catch (TextException e)
                        {
                            page.infoMessage = e.Message;
                        }
                    if (result > 0)
                    {
                        Wild2.Models.Text.Helper.SetLastMessage(message);
                        var url = UrlHelper.GenerateUrl(
                            null,
                            "Index",
                            "Topic",
                            null,
                            null,
                            Math.Abs(id).ToString(),
                            new RouteValueDictionary(new { id = tid }),
                            Url.RouteCollection,
                            Url.RequestContext,
                            false);
                        //return RedirectToAction("Index", new { id = tid });
                        return Redirect(url);
                    }
                }
                else
                {
                    page.infoMessage = "Это сообщение уже отправлено";
                }
                page.Topic = Wild2.Models.Topic.Topic.GetTopicInfo(tid);
                page.Comments = Wild2.Models.Topic.Topic.GetTopicComments(tid);
                if (Request.QueryString["votes"] != null)
                {
                    page.Votes = Wild2.Models.Topic.Topic.GetVotesList(tid);
                }
                return View(page);
            }
            return RedirectToAction("Index", new { id = tid });
        }

        public ActionResult Vote(int topic, int vote, String hash)
        {
            Int32? UserID = Wild2.Models.Auth.AuthOnSite.GetUserId();
            if (UserID.HasValue)
            {
                if (hash == Wild2.Models.Auth.AuthOnSite.GetSessionHash())
                {
                    if (vote < 0) { Wild2.Models.Topic.Topic.VoteMinus(UserID.Value, topic); }
                    if (vote > 0) { Wild2.Models.Topic.Topic.VotePlus(UserID.Value, topic); }
                }
            }
            return RedirectToAction("Index", new { id = topic });
        }
    }
}
